package cn.tedu.pojo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisDemo04 {
    SqlSession session=null;
    /*@Before标记会在每个@Test标记的方法之前执行！*/
    @Before
    public void beforeMethod() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
      //添加true会自动提交事务，默认是false，表示关闭提交
        session = fac.openSession(true);
//        List<Emp> list = session.selectList("EmpMapper.findAll");
//        for (Emp emp : list) {
//            System.out.println(emp);
//        }
    }
    //注解不能写在类里
        @Test
        //新增员工信息：
        public void testInsert(){
            //System.out.println(session);//输出的结果应该为一个地址值
            int rows=session.insert("EmpMapper.insert");
            System.out.println("影响行数为："+rows);
            //提交事务
            session.commit();
        }
         @Test
        public void testUpdate(){
        //通过name
        int rows=session.update("EmpMapper.update");
             System.out.println("影响行数为："+rows);
        }
        @Test
        public void testDelete(){
       int rows=session.delete("EmpMapper.delete");
            System.out.println("影响行数为："+rows);
    }
    /*练习05：新增员工信息：null,马云，教师，800*/
   @Test
    public void testInsert2(){
        //声明一个集合，将SQL参数封装到map中
        Map map= new HashMap();
        map.put("name","马云");
        map.put("job","教师");
        map.put("salary",800);
       int rows= session.insert("EmpMapper.insert2",map);
        System.out.println("影响行数："+rows);
    }
   @Test
    public void testUpdate3(){
       //将SQL语句中的参数封装到Map集合中
        Map<String,Object> map=new HashMap();
        map.put("name", "马云");
        map.put("job", "CEO");
        map.put("salary", 80000);
       //定位SQl并执行SQL
       session.update("EmpMapper.update2",map);
    }

    @Test
    public void testUpdate4(){
        //将SQL语句中的参数封装到Map集合中
        Map<String,Object> map=new HashMap();
        map.put("name", "马云");
        map.put("job", "教授");
        map.put("salary", 888888);
        //定位SQl并执行SQL
        session.update("EmpMapper.update2",map);
    }

    @Test
    public void testDelete2(){
     session.delete("EmpMapper.delete2",18);
    }
    /*练习09：查询emp表中所有的员工信息，动态显示要查询的列*/
     @Test
    public void restFindAll2(){
     Map map=new HashMap();
     map.put("colName", "id,name,job");
     List<Emp>list=
             session.selectList("EmpMapper.findAll2",map);
     for (Emp emp : list){
         System.out.println(emp);
     }
     }

     /*10、根据员工的薪资区间查询员工信息*/
     @Test
     public void testFindBySal2() {
         Map map = new HashMap();
         map.put( "minSal" , 3000 );
         map.put( "maxSal" , 4500 );

         List<Emp> list = session.selectList( "EmpMapper.findBySal2", map );
         for (Emp emp : list) {
             System.out.println( emp );
         }
     }
     //11、批量删除
     @Test
     public void testDeleteByIds() {
         //int[] ids = {1,3,5,7}; //从页面上传过来的员工的id数组
         List list = new ArrayList();
         list.add( 2 );
         list.add( 4 );
         list.add( 6 );
         list.add( 8 );
         session.delete( "EmpMapper.deleteByIds", list );
     }
     @Test
    public void testUpdateByIds(){
         int [] ids={2,4,6,8};
         Double money =1000.0;
         Map map =new HashMap();
         map.put("arrayIds", ids);
         map.put("money", 1000);
         session.update("EmpMapper.updateByIds",map);
     }
}
